import { _decorator, Component, Event } from 'cc';
const { ccclass, menu, property } = _decorator;

@ccclass('Draggable')
@menu('Common/Draggable 拖拽')
export default class Draggable extends Component {
    static EventType = {
        DRAG_START: 'drag_start',
        DRAG_END: 'drag_end',
        DRAG_MOVE: 'drag_move',
    };
    private _dragging = false;
    onEnable() {
        // this.node.on(cc.Node.EventType.TOUCH_MOVE, this._onTouchMoved, this, true);
        // this.node.on(cc.Node.EventType.TOUCH_END, this._onTouchEnded, this, true);
        // this.node.on(cc.Node.EventType.TOUCH_CANCEL, this._onTouchEnded, this, true);
    }
    onDisable() {
        // this.node.off(cc.Node.EventType.TOUCH_MOVE, this._onTouchMoved, this, true);
        // this.node.off(cc.Node.EventType.TOUCH_END, this._onTouchEnded, this, true);
        // this.node.off(cc.Node.EventType.TOUCH_CANCEL, this._onTouchEnded, this, true);
    }
    private _onTouchMoved(e?: Event.EventTouch) {
        // if (!e) {
        // return;
        // }
        // if (!this._dragging) {
        // this._dragging = true;
        // this.node.emit(Draggable.EventType.DRAG_START, this);
        // } else {
        // this.node.emit(Draggable.EventType.DRAG_MOVE, this, e.getLocation());
        // }

        // const camera = cc.Canvas.instance.getComponentInChildren(cc.Camera);
        // const touchLocation = e.touch!.getLocation();
        // const touchPreLocation = e.touch!.getPreviousLocation();
        // let touchPos = camera.getScreenToWorldPoint(touchLocation);
        // let touchPrePos = camera.getScreenToWorldPoint(touchPreLocation);
        // touchPos = this.node.parent.convertToNodeSpaceAR(touchPos);
        // touchPrePos = this.node.parent.convertToNodeSpaceAR(touchPrePos);
        // const deltaPos = touchPos.subtract(touchPrePos);
        // this.node.setPosition(this.node.position.add(deltaPos));

        // e.stopPropagation();
    }
    private _onTouchEnded(e?: Event.EventTouch) {
        // if (this._dragging) {
        // e?.stopPropagation();
        // }
        // this._dragging = false;
        // this.node.emit(Draggable.EventType.DRAG_END, this);
    }
}